package com.yz.group5.med_booking.controller;

import com.yz.group5.med_booking.entity.Appointment;
import com.yz.group5.med_booking.service.AppointmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin // 允许跨域请求
@RestController
@RequestMapping("/appointment")
public class AppointmentController {

    @Autowired
    private AppointmentService appointmentService;

    /**
     * 查看用户的所有挂号记录
     * @param userId 用户ID
     * @return 该用户的所有预约挂号记录列表
     */
    @GetMapping("/user/{userId}")
    public List<Appointment> getUserUserAppointments(@PathVariable Integer userId) {
        return appointmentService.findByUserId(userId);
    }

    /**
     * 创建新的预约挂号
     * @param appointment 包含预约信息的对象
     * @return 预约是否成功
     */
    @PostMapping("/add")
    public boolean addAppointment(@RequestBody Appointment appointment) {





        // 这里可以添加预约时间冲突检查等业务逻辑
        return appointmentService.save(appointment);
    }

    /**
     * 取消预约挂号
     * @param appointmentId 预约记录ID
     * @return 取消是否成功
     */
    @DeleteMapping("/cancel/{appointmentId}")
    public boolean cancelAppointment(@PathVariable Integer appointmentId) {
        return appointmentService.removeById(appointmentId);
    }

    /**
     * 查看所有预约记录（通常用于管理员）
     * @return 所有预约挂号记录列表
     */
    @GetMapping("/all")
    public List<Appointment> getAllAppointments() {
        return appointmentService.listAllAppointments();
    }

    /**
     * 根据科室ID查询该科室的预约记录
     * @param departmentId 科室ID
     * @return 该科室的所有预约挂号记录
     */
    @GetMapping("/department/{departmentId}")
    public List<Appointment> getAppointmentsByDepartment(@PathVariable Integer departmentId) {
        return appointmentService.findByDepartmentId(departmentId);
    }
}
